//
//  THJQihuoIndexVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/6/26.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJQihuoIndexVC.h"
#import "PYDemoOptions.h"
#import "THJQihuoSelecteView.h"
#import "ZBNetworking.h"
@interface THJQihuoIndexVC ()<THJQihuoSelecteViewDelegate>
{
    NSInteger _dayLength;
    NSString * data_type;
}
@property (weak, nonatomic) IBOutlet PYZoomEchartsView *charview;
@property(nonatomic,strong)NSMutableArray * dateArr;
@property(nonatomic,strong)NSMutableArray * dataArray;
@property(nonatomic,strong)NSMutableArray * typeArray;
@property(nonatomic,strong)THJQihuoSelecteView * selectView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSapce;

@end

@implementation THJQihuoIndexVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _dayLength = 29;
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:@"期货"];
    }else{
        [self setNavBarWithTitle:@"期货"];
    }
    self.topSapce.constant = NavbarHeight;
    [self getTypeData];
     self.view.backgroundColor = COLOR(34, 34, 34, 1);
    
    // Do any additional setup after loading the view from its nib.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.title = @"期货指数";
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
-(THJQihuoSelecteView*)selectView{
    if (!_selectView) {
        _selectView = [[THJQihuoSelecteView alloc] initWithArr:self.typeArray];
        NSLog(@"typeStr = %@",self.typeArray);
        _selectView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        _selectView.delegate = self;
    }
    return _selectView;
}
//类型选择
-(void)getTypeData{
    [MBProgressHUD showMessage:@"" toView:self.view];
    __weak typeof(self) weakself = self;
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString = [NSString stringWithFormat:@"http://read.thjb.net/index.php?action=app_data_futures_type&type=%@",self.typeStr];
        NSLog(@"类型-url--%@",request.urlString);
        request.apiType=ZBRequestTypeRefresh;
    }success:^(id responseObject,apiType type){
        NSDictionary * receiveData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        if([receiveData[@"status"] isEqual:@"success"]){
            data_type = receiveData[@"data"][@"type"];
            weakself.typeArray = receiveData[@"data"][@"list"];
        }
        [weakself loadDataWithTypeID:data_type idStr:weakself.typeArray[0][@"id"]];
    } failed:^(NSError *error){
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
    }];
    
    
}
-(void)loadDataWithTypeID:(NSString*)typeId idStr:(NSString*)idStr{
     [MBProgressHUD showMessage:@"正在加载" toView:self.view];
    //日期数组
    _dateArr = [NSMutableArray array];
    //数据数组
    _dataArray = [NSMutableArray array];
    __weak typeof(self) weakself = self;
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString= [NSString stringWithFormat:@"http://read.thjb.net/index.php?action=app_data_futures_list&type=%@&id=%@",typeId,idStr];
        NSLog(@"期货url--%@",request.urlString);
        request.apiType=ZBRequestTypeRefresh;
    }  success:^(id responseObject,apiType type){
        NSDictionary * receiveData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        if([receiveData[@"status"] isEqual:@"success"]) {
            NSString * str = receiveData[@"data"][@"name"];
            [weakself.typeItemBtn setTitle:str forState:UIControlStateNormal];
            if([receiveData[@"data"][@"list"] isEqual:[NSNull null]]){
                [weakself setCharviewWithDayLength:0];
                [weakself HUDShowMsg:@"暂无数据" AfterDelayDimiss:1.5];
                return;
            }
            NSArray * dataArr = receiveData[@"data"][@"list"];
            if(dataArr.count){
                for (NSDictionary * dic in dataArr) {
                    [_dateArr addObject:dic[@"updateTime"]];
                    NSMutableArray * dataArr = [NSMutableArray array];
                    [dataArr addObject: [NSNumber numberWithInteger:[dic[@"todayStartPrice"] integerValue]]];
                    [dataArr addObject: [NSNumber numberWithInteger:[dic[@"todayClosePrice"] integerValue]]];
                    [dataArr addObject: [NSNumber numberWithInteger:[dic[@"min"] integerValue]]];
                    [dataArr addObject: [NSNumber numberWithInteger:[dic[@"max"] integerValue]]];
                    [_dataArray addObject:dataArr];
                }
                [weakself setCharviewWithDayLength:_dayLength];
            }else{
                [weakself setCharviewWithDayLength:0];
            }
            NSLog(@"%@,%@",_dataArray,_dateArr);
        }else{
            [weakself setCharviewWithDayLength:0];
            [weakself HUDShowMsg:@"暂无数据" AfterDelayDimiss:1.5];
        }
    } failed:^(NSError *error){
         [MBProgressHUD hideHUDForView:weakself.view animated:YES];
    }];
}
//五日均线计算
-(NSArray*)getMA5Value{
    NSMutableArray * avragV = [NSMutableArray array];
    NSArray * dataArr = [[_dataArray reverseObjectEnumerator]allObjects];
    for (NSInteger i = 0;i<dataArr.count;i++) {
        if (i>=4) {
            NSInteger temp = 0;
           
            for (NSInteger j = i; j>= i-4; j--) {
                 NSArray * arr = _dataArray[j];
                NSInteger closeValue = [arr[1] integerValue];
                temp += closeValue;
            }
            [avragV addObject:@(temp/5)];
        }else{
           
        }
    }
    NSMutableArray * arr = (NSMutableArray*) [[avragV reverseObjectEnumerator] allObjects];
    for (NSInteger k = 0; k<3; k++) {
        [arr insertObject:@(6400) atIndex:k];
    }
    return arr;
}
-(void)setCharviewWithDayLength:(NSInteger)length{
    NSMutableArray * weekDataArr = (NSMutableArray*) [_dataArray subarrayWithRange:NSMakeRange(0, length)];
    NSMutableArray * weekDateArr = (NSMutableArray*) [_dateArr subarrayWithRange:NSMakeRange(0, length)];
    weekDataArr = (NSMutableArray*) [[weekDataArr reverseObjectEnumerator] allObjects];
    weekDateArr = (NSMutableArray*) [[weekDateArr reverseObjectEnumerator] allObjects];
   // NSArray * ma5Arr = [self getMA5Value];
    PYOption * option = [PYCandlestickDemoOptions candlestick1OptionWithDataArr:_dataArray DateArr:_dateArr MA5DataArr:nil];
    [_charview setOption:option];
    [_charview loadEcharts];
}
- (IBAction)typeItemClicked:(id)sender {
    [self setStateBarColor:SingleColor(clearColor)];
    self.selectView.backgroundColor = COLOR(0, 0, 0, 0.3);
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self.selectView];
}
-(void)selectedType:(NSString *)type infoDic:(NSDictionary *)dic{
    [self loadDataWithTypeID:self.typeStr idStr:dic[@"id"]];
    [self.typeItemBtn setTitle:dic[@"name"] forState:UIControlStateNormal];
}
- (IBAction)btn1clicked:(UIButton*)sender {
    [self setBtnsTitleColor:sender];
    _dayLength = 7;
    [self setCharviewWithDayLength:7];
}
- (IBAction)btn2clicked:(UIButton*)sender {
     [self setBtnsTitleColor:sender];
    _dayLength = 15;
    [self setCharviewWithDayLength:15];
}
- (IBAction)btn3clicked:(UIButton*)sender {
     [self setBtnsTitleColor:sender];
    _dayLength = 29;
    [self setCharviewWithDayLength:29];
}
-(void)setBtnsTitleColor:(UIButton*)sender{
    [self.weekBtn setBackgroundColor:SingleColor(whiteColor)];
    [self.monthBtn setBackgroundColor:SingleColor(whiteColor)];
    [self.moredayBtn setBackgroundColor:SingleColor(whiteColor)];
    [self.weekBtn setTitleColor:COLOR(46, 46, 46, 1.0) forState:UIControlStateNormal];
    [self.monthBtn setTitleColor:COLOR(46, 46, 46, 1.0) forState:UIControlStateNormal];
    [self.moredayBtn setTitleColor:COLOR(46, 46, 46, 1.0) forState:UIControlStateNormal];
    [sender setTitleColor:SingleColor(whiteColor) forState:UIControlStateNormal];
    [sender setBackgroundColor:COLOR(247, 47, 33, 1.0)];
}
@end
